<?php 
	/*==================================================================*/
	/*		文件名FrontAction.class.php                         */
	/*		概要: 所有前台所用到的控制器.                       */
	/*		作者: 高洛峰                                         */
	/*		创建时间: 2010-05-15                                */
	/*		最后修改时间:2010-05-15                             */
	/*		copyright (c)2009 lampteacher@gmail.com             */
	/*==================================================================*/
	//声明FrontAction类继承MyTpl类，这样可以直接使用Smarty类中的属性和方法
	class FrontAction extends MyTpl{
		private $article;
		private $column;
		private $user;

		function __construct(){
			parent::__construct();
			$this->column=new ColumnModel();
			$this->article=new ArticleModel();
			$this->user=new UserModel();
			//分配样式的路径,通过选择不同的模块显示页面
			$this->assign("stylePath", APP_STYLE);
			$this->assign("url","http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
		}
	        /* 这是一个主运行方法，用户所有的访问都是通过这个方法开始运行的
		 * 不需要参数，没有返回值，通过这个方法加载用户的每个执行动作
		 */	
		function run(){
			$act=isset($_GET["a"])?$_GET["a"]:"index";
			//调用用户访问的方法，例如：admin.php?m=user&a=add去访问user模块中的add方法		
			if(method_exists($this,$act)){
				eval('$this->'.$act.'();');  //如果$act为add,相当于$this->add();
			}else{
				echo '没有这个协作！';
			}
		}
		function index(){
			//将网站名称分配到模板中，在标题栏中显示
			$this->assign("appName", APP_NAME);
			$this->assign("keywords", KEYWORD);
			$this->assign("description", DESCRIPTION);
			//获取并分配菜单项
			$this->assign("menu", $this->column->getColumnList(1));
			//幻灯播放的数据分配
			$play=$this->article->getPlayPic(6);	
			$this->assign("play", $play);	
			//获取并分配推荐文章
			$this->assign("recommends", $this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE recommend=1  and audit=1"));
			//获取并分配最新更新文章
			$this->assign("news", $this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE postTime > ".(time()-60*60*24*7)." and audit=1", "postTime"));
			//获取并分配热门文章
			$this->assign("hots",$this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE postTime > ".(time()-60*60*24*30)."  and audit=1", "views desc"));
			//获取并分配所有栏目  
			$this->assign("cols",$this->column->getAllColumn(1));
			//获取并分配友情链接内容
			$flink=new FlinkModel();
			$this->assign("links", $flink->findALL(array("id, webName, url, logo, list"),0,0,"","ord"));   
			$this->display(APP_STYLE."/index.tpl");
		}

		function column(){
			//获取并分配菜单项
			$this->assign("menu", $this->column->getColumnList(1));

			$pid=isset($_GET["pid"])?$_GET["pid"]:1;
			//左边导航
			$this->assign("columnMenu", $this->column->getColumnList($pid));
			//获取本类的推荐文章并分配
			$this->assign("recommends", $this->article->findAll(array("id, title,author,postTime,audit"),0, 13, "WHERE recommend=1  and audit=1 and pid='".$pid."'"));
			//获取本类中热门文章并分配
			$this->assign("hots",$this->article->findAll(array("id, title,author,postTime,audit"),0, 13, "WHERE postTime > ".(time()-60*60*24*30)."  and audit=1 and pid='".$pid."'", "views desc"));
			//调用分页类进行分页显示
			$page=new Page("index.php?a=column&pid=".$pid."&",$this->article->total("where audit=1 and pid={$pid}"), ARTICLE_PAGE_SIZE);	
			
			$sinColumn=$this->column->getColumn($pid,$page->getOffset(), ARTICLE_PAGE_SIZE);
			$this->assign("appName", "栏目：".$sinColumn["title"]);
			$this->assign("keywords",$sinColumn["title"]);
			$this->assign("description", $sinColumn["description"]);
			$this->assign("locs", $this->column->getLocation($sinColumn["path"]));
			$this->assign("column", $sinColumn);
			$this->assign("fpage", $page->getPage());
	 	
			$this->display(APP_STYLE."/column.tpl");
		}

		function article(){
 			//设置访问数量
			$this->article->setViews($_GET["aid"]);      
			
			//获取并分配菜单项
			$this->assign("menu", $this->column->getColumnList(1));
			//获取访问的文章
			$sinArticle=$this->article->find($_GET["aid"]);
			//将文章分配到模板中
			$this->assign("article", $sinArticle);
			//获取并分配将前位置到模板中
			$colPath=$this->column->getCatPath($sinArticle["pid"]);
			$this->assign("locs", $this->column->getLocation($colPath));
			//将网站名称分配到模板中，在标题栏中显示
			$this->assign("appName", $sinArticle["title"]);
			//将这个文章的摘要分配到模板中，在搜索引擎中标题下显示 
			$this->assign("keywords",$sinArticle["keyword"]);
			$this->assign("description", $sinArticle["summary"]);
			//获取和当前文章在同一个类别中的上一篇审核过的文章	
			$this->assign("prevArticle", $this->article->getPrevArticle($sinArticle["pid"],$_GET["aid"]));
			//获取和当前文章在同一个类别中的下一篇审核过的文章
			$this->assign("nextArticle", $this->article->getNextArticle($sinArticle["pid"],$_GET["aid"]));
			//获取同类中的相关文章
			$this->assign("conns", $this->article->findAll(array("id, title,author,postTime"),0, 15, "WHERE audit=1 and pid='".$sinArticle["pid"]."'"));
			//获取本类的推荐文章并分配
			$this->assign("recommends", $this->article->findAll(array("id, title,author,postTime"),0, 10, "WHERE recommend=1  and audit=1 and pid='".$sinArticle["pid"]."'"));
			//获取本类中热门文章并分配
			$this->assign("hots",$this->article->findAll(array("id, title,author,postTime"),0, 10, "WHERE postTime > ".(time()-60*60*24*30)."  and audit=1 and pid='".$sinArticle["pid"]."'", "views desc"));


			$this->assign("aid", $_GET["aid"]);
			$comment=new CommentModel();
			$this->assign("commTotal", $comment->total("where aid='".$_GET["aid"]."'"));
			if($_SESSION["isLoginH"]){
				$user=$this->user->find($_SESSION["uid"]);
				if($user["allow_fbpl"]){
					$this->assign("fbpl", true);
					require "FCKeditor/fckeditor.php";
					$oFCKeditor = new FCKeditor("content") ;          // 创建FCKeditor实例，可创建多个实例
					$oFCKeditor->BasePath = 'FCKeditor/';             // 设置FCKeditor目录地址
					$oFCKeditor->Height= '150';	
					$oFCKeditor->ToolbarSet	= 'Small' ;
					$this->assign("FCK",$oFCKeditor->Create());
				}else{
					$this->assign("fbpl", false);
				}
			}
 			$this->display(APP_STYLE."/article.tpl");
 		
		}
		//输出用户注册表单
		function register(){
			$this->assign("appName", APP_NAME);
			$this->assign("appPath",APP_PATH);
			$url ="http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			$this->assign("url", $url);
			$this->display(APP_STYLE."/register.tpl");
			
		}

		//获取验证码
		function imgcode(){
			$image = new ValidationCode(60,20,4);    
			$image->showImage();
			$_SESSION['validationcode'] =strtoupper($image->getCheckCode()); 
		}
		//检查用户
		function checkuser(){
			if($this->user->total("where username='".$_GET["uname"]."'")>0){
				echo  '<font color="red">该用户名已存在</font>';
			}else{
				echo  '<font color="green">该用户名可以使用</font>';
			}
		}
		//检查用户
		function checkcode(){
			if($_GET["code"]==$_SESSION['validationcode']){
				echo  '<font color="green">验证码输入正确</font>';
			}else{
				echo  '<font color="red">验证码输入错误</font>';
			}
		}

		//用户提交注册的动作到这个方法中处理
		function regpro(){
	
			if($this->user->validateForm()){
				if($this->user->add($_POST)){
					$this->assign("infotitle", "恭喜!用户注册成功");
					$this->assign("url", APP_PATH);
				}else{
					$this->assign("infotitle", "对不起!用户注册失败");
				}
			}else{
					$this->assign("infotitle", "对不起!用户注册失败，原因如下 ");
					$this->assign("error", $this->user->getMessList());
;
			}

			$this->assign("appName", APP_NAME);
			$this->display(APP_STYLE."/message.tpl");
	
		} 
		//用户登录动作
		function login(){
			if($this->user->login($_POST["username"], $_POST["password"])){
				$user=$this->user->find($_SESSION["uid"]);
				if($user["disable"]){
					$this->logout("该用户被禁用.");
				}else{  
					$this->assign("infotitle", "恭喜! 用户<b>".$_POST["username"]."</b>登录成功");	
				}
			}else{
				$this->assign("infotitle", "对不起! 用户<b>".$_POST["username"]."</b>登录失败");
			}
			$this->assign("url", $_POST["url"]);
			$this->assign("appName", APP_NAME);
			$this->display(APP_STYLE."/message.tpl");
		}

		//用户退出动作
		function logout($mess="恭喜! 您已经成功的退出本站系统") {
			$_SESSION = array();
			if (isset($_COOKIE[session_name()])) {
   				setcookie(session_name(), '', time()-42000, '/');
			}
			session_destroy();
			$this->assign("infotitle", $mess);	
			$this->assign("url", APP_PATH);
			$this->assign("appName", APP_NAME);
			$this->display(APP_STYLE."/message.tpl");
		
		}

		function comment(){
			$comment=new CommentModel();
			if(isset($_POST["sub"])){
				if($_GET["action"]=="art")
					$this->assign("url", "index.php?a=article&aid=".$_POST["aid"]);
				else
					$this->assign("url", "index.php?a=comment&aid=".$_POST["aid"]);
		
				if(empty($_POST["content"])){
					$this->assign("infotitle", "发表的评论内容不能为空");
					$this->display(APP_STYLE."/message.tpl");
					exit;
				}else{
					if(!$comment->cadd($_POST)){	
						$this->assign("infotitle", "发表的评论失败！");
						$this->display(APP_STYLE."/message.tpl");
						exit;	
					}
				}
			}
			$article= $this->article->find($_REQUEST["aid"]);
			$this->assign("appName", "用户评论 - ".$article["title"]);
			$this->assign("article",$article);
			$page=isset($_GET["page"])?$_GET["page"]:1;
			$this->assign("page", $page);
			$page=new Page("index.php?a=comment&aid=".$_REQUEST["aid"]."&",$comment->total("where aid='".$_REQUEST["aid"]."'"), ARTICLE_PAGE_SIZE);	
			$this->assign("comment", $comment->getComm($_REQUEST["aid"],$page->getOffset(), ARTICLE_PAGE_SIZE));
			$this->assign("fpage", $page->getPage());
			$dlht=false;
			if($_SESSION["isLoginH"]){
				$user=$this->user->find($_SESSION["uid"]);
				if($user["allow_dlht"]){
					$dlht=true;
				}
				if($user["allow_fbpl"]){
					$this->assign("fbpl", true);
					require "FCKeditor/fckeditor.php";
					$oFCKeditor = new FCKeditor("content") ;          // 创建FCKeditor实例，可创建多个实例
					$oFCKeditor->BasePath = 'FCKeditor/';             // 设置FCKeditor目录地址
					$oFCKeditor->Height= '150';	
					$oFCKeditor->ToolbarSet	= 'Small' ;
					$this->assign("FCK",$oFCKeditor->Create());
				}else{
					$this->assign("fbpl", false);
				}
			}
			$this->assign("dlht", $dlht);
			$this->display(APP_STYLE."/comment.tpl");
		}

		function support(){
			$comment=new CommentModel();
			if($_GET["action"]=="1"){
				echo '支持[<span class="red">'.$comment->setSupport($_GET["cid"], 1).'</span>]';
			}else if($_GET["action"]=="2"){
				echo '反对[<span class="red">'.$comment->setSupport($_GET["cid"], 2).'</span>]';
			}
		}

		function delcomment(){
			$comment=new CommentModel();
			
			if($comment->del($_GET["cid"])){
				$this->comment();
			}else{
				$this->assign("url", "index.php?a=comment&aid=".$_GET["aid"]);
				$this->assign("infotitle", "删除评论失败！");
				$this->display(APP_STYLE."/message.tpl");
			}
		}
		function search(){
			//获取并分配菜单项
			$this->assign("menu", $this->column->getColumnList(1));
			//获取并分配推荐文章
			$this->assign("recommends", $this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE recommend=1  and audit=1"));
			//获取并分配最新更新文章
			$this->assign("news", $this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE postTime > ".(time()-60*60*24*7)." and audit=1", "postTime"));
			//获取并分配热门文章
			$this->assign("hots",$this->article->findAll(array("id, title,author,postTime,audit"),0, 10, "WHERE postTime > ".(time()-60*60*24*30)."  and audit=1", "views desc"));
			$search=rtrim($_REQUEST["search"]);
			if(!empty($search)){	
				$total=$this->article->getSearchTotal($_REQUEST["serType"], $search);
				$page=new Page("index.php?a=search&serType=".$_REQUEST["serType"]."&search=".$search."&",$total, ARTICLE_PAGE_SIZE);	
			
				$sArticle=$this->article->getSearchResult($_REQUEST["serType"], $search,$page->getOffset(),  ARTICLE_PAGE_SIZE);
				$this->assign("searchs", $sArticle);
				$this->assign("search", $search);
				$this->assign("serType", $_REQUEST["serType"]);
				$this->assign("total", $total);
				$this->assign("fpage", $page->getPage());
			}
			//获取并分配所有栏目  
			$this->display(APP_STYLE."/search.tpl");
		}
	}
